﻿using System;
using System.Collections.Generic;

namespace IcicleFramework.Collision
{
    public class CollisionList
    {
        private List<ICollision> collisionObjects;

        public int Count
        {
            get { return collisionObjects.Count; }
        }

        public ICollision this[int index]
        {
            get { return collisionObjects[index]; }
        }

        public ICollision this[string name]
        {
            get
            {
                int i;

                for (i = 0; i < collisionObjects.Count; i++)
                {
                    if (name.Equals(collisionObjects[i].Name, StringComparison.InvariantCulture))
                        return collisionObjects[i];
                }

                return null;
            }
        }
        public CollisionList()
        {
            collisionObjects = new List<ICollision>();
        }

        public void AddCollisionData(ICollision collisionData)
        {
            if (!collisionObjects.Contains(collisionData))
            {
                collisionObjects.Add(collisionData);
            }
        }

        public bool IsColliding(CollisionList otherList)
        {
            bool colliding = false;
            int i, j;

            for (i = 0; i < collisionObjects.Count && !colliding; i++)
            {
                if (collisionObjects[i].Active)
                {
                    for (j = 0; j < otherList.Count && !colliding; j++)
                    {
                        if(otherList[i].Active)
                        {
                            if (collisionObjects[i].IsColliding(otherList[i]))
                                colliding = true;
                        }
                    }
                }
            }

            return colliding;
        }
    }
}
